package jp.co.cos.ittrick.channel.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jp.co.cos.ittrick.channel.meta.PreModelMeta;
import jp.co.cos.ittrick.channel.meta.TableModelMeta;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModelRef;
import org.slim3.datastore.Sort;
import org.slim3.datastore.json.Json;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Text;

@Model(schemaVersion = 1)
public class PageModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /** キー（INT） */
    @Attribute(primaryKey = true, json=@Json(ignore=true))
    private Key key;

    /** バージョン */
    @Attribute(version = true, json=@Json(ignore=true))
    private Long version;

    /** Class名 */
    private String name;

    /** 表示順 */
    private int index;

    /** コンテンツ */
    private Text content;

    /** 公開フラグ */
    private Boolean publicFlg = false;

    /** 公開日時 */
    private Date publicDate;

    /** キーワードリスト */
    private List<String> keyWordList;

    /** Meta Description */
    private String description;

    /** 更新日時 */
    private Date lastmod;

    /** ChapterModel に対しての関連 */
    @Attribute(json=@Json(ignore=true))
    private ModelRef<ChapterModel> chapterModelRef = new ModelRef<ChapterModel>(ChapterModel.class);

    /** PreModelに対しての関連 */
    @Attribute(persistent = false)
     private InverseModelListRef<PreModel, PageModel> preModelListRef =
        new InverseModelListRef<PreModel, PageModel>(
                PreModel.class,
                PreModelMeta.get().pageModelRef.getName(),
                this,
                new Sort(PreModelMeta.get().index, SortDirection.ASCENDING) );

    /** TableModelに対しての関連 */
    @Attribute(persistent = false)
     private InverseModelListRef<TableModel, PageModel> tableModelListRef =
        new InverseModelListRef<TableModel, PageModel>(
                TableModel.class,
                TableModelMeta.get().pageModelRef.getName(),
                this);

  //--------------------永久化しない項目---------------------------------
    @Attribute(persistent = false)
    public String pageUrlString;

    public long getKeyString() {
        return key.getId();
    }

    public String getContentString() {
        if(content != null && content.getClass() != null) {
            return content.getValue();
        }else {
            return "";
        }
    }

    public String getLastmodOfFormat() {
        SimpleDateFormat fm1 = new SimpleDateFormat("yyyy-MM-dd");
        return fm1.format(lastmod);
    }

    public String getKeyWordListString() {

        String keyWordString = "";
        for(int i = 0; i < keyWordList.size(); i++) {
            if(i == 0) {
                keyWordString = keyWordList.get(i);
            }else {
                keyWordString = keyWordString + "," + keyWordList.get(i);
            }
        }
        return keyWordString;
    }

    @Attribute(persistent = false)
    private String keyValueString;

    public String getKeyValueString() {
        return String.valueOf(key.getId());
    }

//----------------------メソッド---------------------------------------

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        PageModel other = (PageModel) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public void setLastmod(Date lastmod) {
        this.lastmod = lastmod;
    }

    public Date getLastmod() {
        return lastmod;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setKeyWordList(List<String> keyWordList) {
        this.keyWordList = keyWordList;
    }

    public List<String> getKeyWordList() {
        return keyWordList;
    }

    public void setContent(Text content) {
        this.content = content;
    }

    public Text getContent() {
        return content;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getIndex() {
        return index;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPublicDate(Date publicDate) {
        this.publicDate = publicDate;
    }

    public Date getPublicDate() {
        return publicDate;
    }

    public void setPublicFlg(Boolean publicFlg) {
        this.publicFlg = publicFlg;
    }

    public Boolean getPublicFlg() {
        return publicFlg;
    }

    public ModelRef<ChapterModel> getChapterModelRef() {
        return chapterModelRef;
    }

    public InverseModelListRef<PreModel, PageModel> getPreModelListRef() {
        return preModelListRef;
    }

    public InverseModelListRef<TableModel, PageModel> getTableModelListRef() {
        return tableModelListRef;
    }
}
